<?php
Core::getInstance()->sys_load_class('Model',false);
Core::getInstance()->app_load_class('Admin',false);
Core::getInstance()->sys_load_class('Character',false);
class Index extends Admin{
	/*Singleton{{{*/
	public static function getInstance(){
		static $instance=null;
		if($instance===null){ 
			$instance = new self();
		}
		return $instance;
	}
	private function __construct(){}
	private function __clone(){
		trigger_error("Clone is not allowed.", E_USER_ERROR);
	}
	/*}}}*/

	public function indexAction(){
		/*$str = 'zhoubc/opt/data/phpcms/uploadfile/2011/0523/20110523022952162.jpeg';
		$pos = strrpos($str,'/')+1;
		$str = substr($str,0,$pos).'thumb_'.substr($str,$pos);
		echo $str;*/
		include $this->display('index.tpl.php');
	}

	// 板块数据
	public function dataListAction(){
		$title='板块数据';
		$wdb = Core::getInstance()->app_load_class('WebDataBlock');
		if(isset($_GET['o']) && $_GET['o']==='del' && isset($_GET['id'])){
			$wdb->delData($_GET['id']);
		}
		$data = $wdb->lists();
		include $this->display('data_list.tpl.php');
	}

	// 数据添加页面
	public function datablockAction(){
		$title = '添加板块';
		if(isset($_GET['id'])){
			$title = '更新板块';
			$id = intval($_GET['id']);
			$wdb = Core::getInstance()->app_load_class('WebDataBlock');
			$data = $wdb->getOneData($id);
		}
		include $this->display('data_index.tpl.php');
	}

	// 更新数据
	public function dataUpdateAction(){
		$title = '更新板块数据';
		$wdb = Core::getInstance()->app_load_class('WebDataBlock');
		if(!empty($_POST['name'])){
			if(strpos($_SERVER['HTTP_REFERER'],DOMAIN)!==false){ // 外部攻击
				if(isset($_POST['id'])){
					$id=intval($_POST['id']);
					$wdb->updateData($id);
					header('Location:?m=admin&c=index&a=dataList');
				}else
					$id = $wdb->addData();
			}else{
				Core::showmessage('Invalid Operate');
			}
		}else{
			if(isset($_GET['id'])){
				$id=intval($_GET['id']);
			}else
				Core::showmessage('Invalid Operate');
		}
		$data = $wdb->getOneData($id);

		include $this->display('data_update.tpl.php');
	}

	// Add Data
	public function dataAddAction(){
		$wdb = Core::getInstance()->app_load_class('WebDataBlock');
		if(isset($_POST['id'])){
			$json = json_encode($_POST);
			$json = str_replace("\u",'%2F',$json);
			$id = intval($_POST['id']);
			$wdb->updateJson($json,$id);
			header('Location:?m=admin&c=index&a=dataList');
		}else{
			if(isset($_GET['id'])){
				$id=intval($_GET['id']);
			}else
				Core::showmessage('Invalid Operate');

			$title = '更新板块模版';
			$data = $wdb->getOneData($id);
			include $this->display('data_tpl.tpl.php');
		}
	}

	// Add tpl 
	public function dataTplAction(){
		$wdb = Core::getInstance()->app_load_class('WebDataBlock');
		if(!isset($_POST['id']) || !isset($_POST['tpl'])){
			Core::showmessage('Invalid Operate');
		}

		$id=intval($_POST['id']);
		$tpl = $_POST['tpl'];

		$wdb->updateTpl($tpl,$id);


		header('Location:?m=admin&c=index&a=dataList');
	}

	// 预览
	public function dataReviewAction(){
		$wdb = Core::getInstance()->app_load_class('WebDataBlock');
		if(!isset($_GET['id'])){
			Core::showmessage('Invalid Operate');
		}

		$id=intval($_GET['id']);
		$data = $wdb->getOneData($id);
		echo Character::HTMLToDbDecode($data['html']);
	}


	// 关联生成模版
	public function relateHTML($id){
		$wdb = Core::getInstance()->app_load_class('WebDataBlock');
		$id=intval($id);
		$data = $wdb->getOneData($id);
		if(empty($data['tpl'])) return false;

		$tpl = $data['tpl'];

	}

}
